package ModelLayer;

import java.util.ArrayList;
/**
 * This class makes an arraylist which can contains DVD objects.
 * 
 * 
 * 
 * @author (Gruppe C) 
 * @version (12-04-2011)
 */
public class DVDCollection
{
    // instance variables - replace the example below with your own
    private ArrayList<DVD> dvdCollection;
    private static DVDCollection _instance = null;
    
    private DVDCollection()
    {
        dvdCollection = new ArrayList<DVD>();
    }
    
    public static DVDCollection getInstance()
    {
        if(null == _instance) {
            _instance = new DVDCollection();
        }
        return _instance;
    }
    
    /**
     * Add a dvd to arraylist
     * @param newDvd DVD object
     */
    public void addDvd(DVD newDvd)
    {
        dvdCollection.add(newDvd);
    }
    
    /**
     * Remove the current dvd from the arraylist.
     * @param DVD the dvd you want to remove.
     */
    public void removeDvd(DVD dvd)
    {
        dvdCollection.remove(dvd.getId());
    }
    
    /**
     * Remove a dvd from arraylist
     * @param id uses the id to find the dvd which have to be removed
     * @return found found is a boolean.
     */
    public boolean removeDvd(int id)
    {
        int index = 0;
        boolean found = false;
        while(index < dvdCollection.size() && !found) {
            int dvdId = dvdCollection.get(index).getId();
            if(dvdId == id) {
                found = true;
                dvdCollection.remove(index);
            }
            else {
                index++;
            }
        }
        return found;
    }
    
    /**
     * Search for a dvd in the arraylist.
     * @param title the title you want to search after.
     * @return if found, returns an object of the dvd. if not found, returns null.
     */
    public DVD searchDvd(String title)
    {
        int index = 0;
        boolean found = false;
        
        while(index < dvdCollection.size() && !found) {
            String dvdTitle = dvdCollection.get(index).getTitle();
            if(dvdTitle.equals(title)) {
                found = true;
                DVD searchDvd = dvdCollection.get(index);
                return searchDvd;
            }
            else {
                index++;
            }
        }
        return null;
    }

    public DVD getDvd(int id)
    {
        int index = 0;
        boolean found = false;
        
        while(index < dvdCollection.size() && !found) {
            int dvdId = dvdCollection.get(index).getId();
            if(id == dvdId) {
                found = true;
                DVD searchDvd = dvdCollection.get(index);
                return searchDvd;
            }
            else {
                index++;
            }
        }
        return null;
    }
 }
